var _util = require('util/util.js');

var _catalog_service = {
  //向服务器发送请求，获取所有的category
  getCategories : function(resolve, reject){
    console.log('getCategories')
    _util.request({
        url : _util.getServerURL('catalog/categories'),
        method : 'GET',
        success : resolve,
        error : reject
    })
  },

  //向服务器发送请求，根据categoryId获取所有的product
  getProductsByCategoryId : function(categoryId, resolve, reject){
    console.log('getProductsByCategoryId')
    _util.request({
        url : _util.getServerURL(`catalog/categories/${categoryId}/products`),
        method : 'GET', 
        success : resolve,
        error : reject
    })
  },

  //向服务器发送请求，根据productId获取product
  getProductByProductId : function(productId, resolve, reject){
    console.log('getProductByProductId')
    _util.request({
        url : _util.getServerURL(`catalog/products/${productId}`),
        method : 'GET', 
        success : resolve,
        error : reject
    })
  },

  //向服务器发送请求，根据productId获取所有的item
  getItemsByProductId : function(productId, resolve, reject){
    console.log('getItemsByProductId')
    _util.request({
        url : _util.getServerURL(`catalog/products/${productId}/items`),
        method : 'GET',
        success : resolve,
        error : reject
    })
  },

  //向服务器发送请求，根据itemId获取item信息
  getItemById : function(itemId, resolve, reject){
    console.log('getItemById')
    _util.request({
        url : _util.getServerURL(`catalog/items/${itemId}`),
        method : 'GET',
        success : resolve,
        error : reject
    })
  },

  //向服务器发送请求，根据关键字获取搜索的Product
  getProductByKey:function (keyword,resolve,reject) {
    _util.request({
        url : _util.getServerURL('catalog/products/search'),
        data:keyword,
        method : 'GET',
        success : resolve,
        error : reject
    })

    }
};

module.exports = _catalog_service;